<?php

namespace App\Models;


use Illuminate\Database\Eloquent\Model;

class ShopContract extends Model
{
    protected $table = 'shop_contract';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'shopid',
        'contractorid',
        'starttime',
        'lasttime',
        'eventdays',
        'statementeventdays',
        'commission',
        'newshoppercentage',
        'performancepercentage',
        'halfdeductions',
        'totaldeductions',
        'deductionstatus',
    ];

    //商品下架或取消翻倍日之前都应当执行该方法，shopid为必传，type为0时不涉及到扣除方法，1为扣一半，2为扣全部
    public static function deductionModel($shopId, $type=0) {
        if(!$shopId) {
            return false;
        }
        if(Shop::where('id',$shopId)->pluck('todayevent')->first()) {
            Shop::where('id',$shopId)->update(['todayevent'=>'0']);
            if(ShopEvent::where('shopid',$shopId)->where('event',date('Y-m-d',time()))->get()) {
                ShopEvent::where('shopid',$shopId)->where('event',date('Y-m-d',time()))->delete();
                ShopHistoryEvent::where('shopid',$shopId)->where('event',date('Y-m-d',time()))->delete();
                self::where('shopid', $shopId)->where('starttime','<=',date('Y-m-d',time()))->where('lasttime','>=',date('Y-m-d',time()))->where('eventdays','>','0')->decrement('eventdays',1);
            }
        }
        if($type) {
            self::where('shopid',$shopId)->where('starttime','<=',date('Y-m-d',time()))->where('lasttime','>=',date('Y-m-d',time()))->update(['deductionstatus'=>$type]);
        }
        return true;
    }

    //商户签约模型
    public static function shopContractModel($shopid,$contractorid,$starttime,$lasttime)
    {
        if(!$shopid || !$starttime || !$lasttime || !$contractorid)
        {
            return false;
        }
        if(strtotime($starttime) > strtotime($lasttime))
        {
            return false;
        }
        $contract = self::where('shopid',$shopid)->where('starttime','<=',date('Y-m-d',strtotime($starttime)))->where('lasttime','>=',date('Y-m-d',strtotime($starttime)))->get();
        if($contract)
        {
            self::where('shopid',$shopid)->where('starttime','<=',date('Y-m-d',strtotime($starttime)))->where('lasttime','>=',date('Y-m-d',strtotime($starttime)))->update(['lasttime'=>date('Y-m-d',(strtotime($starttime) - 86400))]);
        }
        $newcontract = new self;
        $newcontract->starttime = $starttime;
        $newcontract->lasttime = $lasttime;
        $newcontract->contractorid = $contractorid;
        $newcontract->shopid = $shopid;
        $newcontract->save();
        return true;
    }

    //商家签约时新增一条插入数据
    public static function addShopContractModel($shopId,$contractorId,$startTime,$lastTime){
        if(!$shopId || !$startTime || !$lastTime || !$contractorId) {
            return ['status'=>'failed','message'=>'续签失败'];;
        }
        if(strtotime($startTime) > strtotime($lastTime)) {
            return ['status'=>'failed','message'=>'续签失败'];
        }
        $shopUser = self::create([
            'shopid' => $shopId,
            'contractorid' => $contractorId,
            'starttime' =>  $startTime,
            'lasttime' =>  $lastTime,
        ]);
        $shopUser->save();
        return ['status'=>'success','message'=>'添加续约成功'];
    }
}
